#include "dataSource.h"
#include <cstring>
#include <math.h>
#include <QDebug>
double DataSource::pi = 3.14159265358979323846;
DataSource::DataSource()
{
    mid = 0;
    sid = 0;
    bid = 0;
    flag = 0;
    reserve = 0;
}

bool DataSource::resolvePdxpData(const char *src, int srcLength, Type type, CoordXYZ *pos, int64_t *t)
{
    unsigned short dateIn = 0;
    unsigned int timeIn = 0;

    int longitudeIn = 0;
    int latitudeIn = 0;
    int heightIn = 0;
    int xIn = 0;
    int yIn = 0;
    int zIn = 0;
    CoordLBH lbh;
    QDate date;
    QTime time;

    switch (type) {
    case GPS:
        //取数据中GPS日期
        if(srcLength < 23)
            return false;
        memcpy(&dateIn,src,2);
        date = QDate(2000,1,1).addDays(dateIn-1);
        //取数据中GPS定位时间
        memcpy(&timeIn,src+6,4);
        time = QTime(0,0,0).addMSecs(timeIn);
        *t = QDateTime(date,time).toMSecsSinceEpoch()*1000;
        //取数据中GPS定位
        memcpy(&longitudeIn, src+11, 4);
        memcpy(&latitudeIn, src+15, 4);
        memcpy(&heightIn, src+19, 4);
        lbh.set(longitudeIn*1e-7, latitudeIn*1e-7, heightIn*0.1);
        *pos = lbh.toXYZ();
        break;
    case BD:
        if(srcLength < 25)
            return false;
        //取日期和时间
        memcpy(&dateIn, src, 2);
        memcpy(&timeIn, src+6, 4);
        date = QDate(2000,1,1).addDays(dateIn-1);
        time = QTime(0,0,0).addMSecs(timeIn);
        *t = QDateTime(date,time).toMSecsSinceEpoch()*1000;
        //取经度，纬度，高度
        memcpy(&longitudeIn, src+13, 4);
        memcpy(&latitudeIn, src+17, 4);
        memcpy(&heightIn, src+21, 4);
        lbh.set(longitudeIn*1e-7, latitudeIn*1e-7, heightIn*0.1);
        *pos = lbh.toXYZ();
        break;
    case XYZ:
        if(srcLength < 16)
            return false;
        memcpy(&timeIn, src, 4);

        *t = ((int64_t)timeIn)*100;
        memcpy(&xIn,src+4,4);
        memcpy(&yIn,src+8,4);
        memcpy(&zIn,src+12,4);
        pos->set(xIn*0.1, yIn*0.1, zIn*0.1);
        break;
    default:
        return false;
    }
    return true;
}

bool DataSource::resolveRadarData(const char *src, int srcLength,const CoordLBH &local, std::map<unsigned char,RadarPos> *posMap)
{

    if(srcLength < 27)
    {
        return false;
    }else
    {
        unsigned char targetNum = 0;
        targetNum = src[0];
        if(srcLength < targetNum*26+1)
            return false;
        uint32_t tIn = 0;
        uint32_t vIn = 0;
        uint32_t rIn = 0;
        double r = 0.0;
        int aIn = 0;
        double a = 0.0;
        int eIn = 0;
        double e = 0.0;
        unsigned char targetNo = 0;
        char zt = 0;
        CoordRAE pos;
        for(int i = 0; i < targetNum; i++)
        {
            targetNo = src[i*26+1];
            memcpy(&tIn, 1+src+i*26 +1,4);
            memcpy(&zt,  1+src+i*26 +5,1);
            memcpy(&vIn, 1+src+i*26 +6,4);
            memcpy(&rIn, 1+src+i*26 +10,4);
            memcpy(&aIn, 1+src+i*26 +14,4);
            memcpy(&eIn, 1+src+i*26 +18,4);
            r = rIn*0.01;
            a = aIn*360.0/pow(2,31)*pi/180.0;
            e = eIn*360.0/pow(2,31)*pi/180.0;
            pos.set(r,a,e);
            RadarPos radarPos{tIn,pos.toXYZ(local), zt};
            (*posMap)[targetNo]=radarPos;
        }
    }
    return true;
}

bool DataSource::isSourceData(int option, const PdxpHead &head)
{

    if((option&Mid)==Mid)
    {
        if(head.getOpt(PdxpHead::MID)!=mid)
            return false;
    }
    if((option&Sid)==Sid)
    {
        if(head.getOpt(PdxpHead::SID)!=sid)
        {
            return false;
        }
    }
    if((option&Bid)==Bid)
    {
        if(head.getOpt(PdxpHead::BID)!=bid)
            return false;
    }
    if((option&Flag)==Flag)
    {
        if(head.getOpt(PdxpHead::FLAG)!=flag)
            return false;
    }
    if((option&Reserve)==Reserve)
    {
        if(head.getOpt(PdxpHead::Reserve)!=reserve)
            return false;
    }
    return true;
}

bool DataSource::resolvePdxpFrame(const char *src, int length, Type type, QList<FrameInf> &infList)
{
    uint8_t data8;
    uint16_t data16;
    uint32_t data32;

    infList.clear();
    FrameInf inf;
    if(type == GPS)
    {
        if(length < 23)
            return false;
        inf.name = "帧发送日期 单位：天";
        inf.size= 2;
        memcpy(&data16, src, 2);
        inf.hexByteStr = QString::number(data16, 16);
        inf.value = QDate(2000, 1, 1).addDays(data16-1).toString("yyyy-MM-dd");
        infList<<inf;

        inf.name = "帧发送时间 单位：0.1ms";
        inf.size = 4;
        memcpy(&data32, src+2, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QTime(0,0,0).addMSecs(data32/10).toString("h:m:s zzz")+QString::number(data32%10);
        infList<<inf;

        inf.name = "GPS定位时间 单位：1ms";
        inf.size = 4;
        memcpy(&data32, src+6, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QTime(0,0,0).addMSecs(data32).toString("h:m:s zzz");
        infList<<inf;

        inf.name = "GPS状态 0x07为异常值";
        inf.size = 1;
        memcpy(&data8, src+10, 1);
        inf.hexByteStr = QString::number(data8, 16);
        inf.value = "-";
        infList<<inf;


        inf.name = "东经，单位：e-7rad";
        inf.size = 4;
        memcpy(&data32, src+11, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*1e-7);
        infList<<inf;

        inf.name = "北纬，单位：e-7rad";
        inf.size = 4;
        memcpy(&data32, src+15, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*1e-7);
        infList<<inf;

        inf.name = "高度，单位：0.1m";
        inf.size = 4;
        memcpy(&data32, src+19, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*0.1);
        infList<<inf;
        return true;
    }

    if(type == BD)
    {
        if(length < 25)
            return false;
        inf.name = "帧发送日期 单位：天";
        inf.size= 2;
        memcpy(&data16, src, 2);
        inf.hexByteStr = QString::number(data16, 16);
        inf.value = QDate(2000, 1, 1).addDays(data16-1).toString("yyyy-MM-dd");
        infList<<inf;

        inf.name = "帧发送时间 单位：0.1ms";
        inf.size = 4;
        memcpy(&data32, src+2, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QTime(0,0,0).addMSecs(data32/10).toString("h:m:s zzz")+QString::number(data32%10);
        infList<<inf;

        inf.name = "北斗定位时间 单位：1ms";
        inf.size = 4;
        memcpy(&data32, src+6, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QTime(0,0,0).addMSecs(data32).toString("h:m:s zzz");
        infList<<inf;

        inf.name = "北斗IC卡状态";
        inf.size = 1;
        memcpy(&data8, src+10, 1);
        inf.hexByteStr = QString::number(data8, 16);
        inf.value = "-";
        infList<<inf;

        inf.name = "北斗硬件状态";
        inf.size = 1;
        memcpy(&data8, src+111, 1);
        inf.hexByteStr = QString::number(data8, 16);
        inf.value = "-";
        infList<<inf;

        inf.name = "北斗IC卡状态";
        inf.size = 1;
        memcpy(&data8, src+12, 1);
        inf.hexByteStr = QString::number(data8, 16);
        inf.value = "-";
        infList<<inf;


        inf.name = "东经，单位：e-7rad";
        inf.size = 4;
        memcpy(&data32, src+13, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*1e-7);
        infList<<inf;

        inf.name = "北纬，单位：e-7rad";
        inf.size = 4;
        memcpy(&data32, src+17, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*1e-7);
        infList<<inf;

        inf.name = "高度，单位：0.1m";
        inf.size = 4;
        memcpy(&data32, src+21, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*0.1);
        infList<<inf;
        return true;
    }

    if(type == XYZ)
    {
        if(length!=28 && length!=16)
        {
            return false;
        }

        inf.name = "数据绝对时，无符号整数，单位：0.1ms";
        inf.size = 4;
        memcpy(&data32, src, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QTime(0,0,0).addMSecs(data32*0.1).toString("h:m:s zzz")+QString::number(data32%10);
        infList<<inf;

        inf.name = "X坐标，有符号整数，单位：0.1m";
        inf.size = 4;
        memcpy(&data32, src+4, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*0.1);
        infList<<inf;

        inf.name = "Y坐标，有符号整数，单位：0.1m";
        inf.size = 4;
        memcpy(&data32, src+8, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*0.1);
        infList<<inf;

        inf.name = "Z坐标，有符号整数，单位：0.1m";
        inf.size = 4;
        memcpy(&data32, src+12, 4);
        inf.hexByteStr = QString::number(data32, 16);
        inf.value = QString::number((int)data32*0.1);
        infList<<inf;

        if(length == 28)
        {
            inf.name = "Z坐标，有符号整数，单位：0.1m";
            inf.size = 4;
            memcpy(&data32, src+16, 4);
            inf.hexByteStr = QString::number(data32, 16);
            inf.value = QString::number((int)data32*0.01);
            infList<<inf;

            inf.name = "Z坐标，有符号整数，单位：0.1m";
            inf.size = 4;
            memcpy(&data32, src+20, 4);
            inf.hexByteStr = QString::number(data32, 16);
            inf.value = QString::number((int)data32*0.01);
            infList<<inf;

            inf.name = "Z坐标，有符号整数，单位：0.1m";
            inf.size = 4;
            memcpy(&data32, src+24, 4);
            inf.hexByteStr = QString::number(data32, 16);
            inf.value = QString::number((int)data32*0.01);
            infList<<inf;
        }
        return  true;
    }

    if(type == Radar)
    {
        if(length < 27)
            return false;
        inf.name = "无符号整数，目标数";
        inf.size= 1;
        memcpy(&data8, src, 1);
        inf.hexByteStr = QString::number(data8, 16);
        inf.value = QString::number(data8);
        infList<<inf;
        if(length<data8*26+1)
            return false;
        int targetNum = data8;
        for(int iTarget = 0; iTarget < targetNum; iTarget++)
        {
            inf.name = "目标号";
            inf.size = 1;
            memcpy(&data8, src+1+iTarget*26, 1);
            inf.hexByteStr = QString::number(data8, 16);
            inf.value = QString::number(data8);
            infList<<inf;

            inf.name = "目标信息:数据绝对时,整数，单位：0.1ms";
            inf.size = 4;
            memcpy(&data32, src+2+iTarget*26, 4);
            inf.hexByteStr = QString::number(data32, 16);
            inf.value = QTime(0,0,0).addMSecs(data32*0.1).toString("h:m:s zzz")+QString::number(data32%10);
            infList<<inf;

            inf.name = "目标信息：状态码,第0位为0为丢失目标";
            inf.size = 1;
            memcpy(&data8, src+2+4+iTarget*26, 1);
            inf.hexByteStr = QString::number(data8, 16);
            inf.value = "-";
            infList<<inf;

            inf.name = "目标信息：速度，有符号整数，单位：0.001m/s";
            inf.size = 4;
            memcpy(&data32, src+2+5+iTarget*26, 4);
            inf.hexByteStr = QString::number(data32, 16);
            inf.value = QString::number((int)data32*0.001);
            infList<<inf;

            inf.name = "目标信息：径向距离，无符号整数，单位：0.01m";
            inf.size = 4;
            memcpy(&data32, src+2+9+iTarget*26, 4);
            inf.hexByteStr = QString::number(data32, 16);
            inf.value = QString::number(data32*0.01);
            infList<<inf;


            inf.name = "目标信息：方位角，有符号整数，单位：360°/2^31";
            inf.size = 4;
            memcpy(&data32, src+2+13+iTarget*26, 4);
            inf.hexByteStr = QString::number(data32, 16);
            inf.value = QString::number((int)data32*360/pow(2,31));
            infList<<inf;

            inf.name = "目标信息：仰角，有符号整数，单位：360°/2^31";
            inf.size = 4;
            memcpy(&data32, src+2+17+iTarget*26, 4);
            inf.hexByteStr = QString::number(data32, 16);
            inf.value = QString::number((int)data32*360/pow(2,31));
            infList<<inf;

            inf.name = "目标信息：方位角动态滞后修正量，有符号整数，单位：1°/2^7";
            inf.size = 1;
            memcpy(&data8, src+2+21+iTarget*26, 4);
            inf.hexByteStr = QString::number(data8, 16);
            inf.value = QString::number((int)data8*pow(2,-7));
            infList<<inf;

            inf.name = "目标信息：仰角动态滞后修正量，有符号整数，单位：1°/2^7";
            inf.size = 1;
            memcpy(&data8, src+2+22+iTarget*26, 4);
            inf.hexByteStr = QString::number(data8, 16);
            inf.value = QString::number((int)data8*pow(2,-7));
            infList<<inf;

            inf.name = "目标信息：AGC，无符号整数，单位：0.5dB";
            inf.size = 1;
            memcpy(&data8, src+2+23+iTarget*26, 4);
            inf.hexByteStr = QString::number(data8, 16);
            inf.value = QString::number(data8*0.5);
            infList<<inf;

            inf.name = "目标信息：窄带RCS，有符号整数，0.5dBm²";
            inf.size = 1;
            memcpy(&data8, src+2+24+iTarget*26, 4);
            inf.hexByteStr = QString::number(data8, 16);
            inf.value = QString::number((int)data8*0.5);
            infList<<inf;
        }


        return true;
    }
    return true;
}
